/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CorrelationLinePlotInternalFrame.java
 *
 * Created on 18.03.2010, 09:09:53
 */
package GUI.iFrames;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JPopupMenu;

import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.LegendItemEntity;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.util.ShapeUtilities;

import GUI.ChartTools.legendItemEditorPopup;

/**
 * 
 * @author Bunkowski
 */
public class CorrelationLinePlotInternalFrame extends javax.swing.JInternalFrame implements MouseListener,
		ChartMouseListener, MouseWheelListener, MouseMotionListener {

	public static final long serialVersionUID = 1L;

	private XYPlot xyp;
	private Point2D panStartPoint;
	private ChartPanel cp;
	private XYSeriesCollection sc;

	/** Creates new form CorrelationLinePlotInternalFrame */
	public CorrelationLinePlotInternalFrame() {
		initComponents();
	}

	public void showXYChart(XYSeriesCollection sc, String xAxisName, String yAxisName) {
		this.sc = sc;
		center.removeAll();

		NumberAxis xAxis = new NumberAxis(xAxisName);
		NumberAxis yAxis = new NumberAxis(yAxisName);
		XYLineAndShapeRenderer lineR = new XYLineAndShapeRenderer(false, true);
		lineR.setSeriesLinesVisible(0, true);
		lineR.setSeriesShapesVisible(0, false);

		xyp = new XYPlot(sc, xAxis, yAxis, lineR);

		JFreeChart chart = new JFreeChart(xyp);
		cp = new ChartPanel(chart);
		center.add(cp);
		cp.addMouseWheelListener(this);
		cp.addChartMouseListener(this);
		cp.addMouseMotionListener(this);
		cp.addMouseListener(this);

		validate();
	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
	 * content of this method is always regenerated by the Form Editor.
	 */
	public void mousePressed(MouseEvent e) {
		if (e.getButton() == 2) {
			cp.setDomainZoomable(false);
			cp.setRangeZoomable(false);
			Rectangle2D dataArea = cp.getScreenDataArea();
			Point2D point = e.getPoint();
			if (dataArea.contains(point)) {
				this.panStartPoint = point;
			}
		}

	}

	public void mouseReleased(MouseEvent e) {
		this.panStartPoint = null;
		cp.setDomainZoomable(true);
		cp.setRangeZoomable(true);
	}

	public void mouseDragged(MouseEvent e) {
		if (this.panStartPoint != null) {
			Rectangle2D scaledDataArea = this.cp.getScreenDataArea();

			this.panStartPoint = ShapeUtilities.getPointInRectangle(this.panStartPoint.getX(),
					this.panStartPoint.getY(), scaledDataArea);
			Point2D panEndPoint = ShapeUtilities.getPointInRectangle(e.getX(), e.getY(), scaledDataArea);

			// horizontal pan

			XYPlot hvp = cp.getChart().getXYPlot();
			ValueAxis xAxis = hvp.getDomainAxis();

			if (xAxis != null) {
				double translatedStartPoint = xAxis.java2DToValue((float) this.panStartPoint.getX(), scaledDataArea,
						hvp.getDomainAxisEdge());
				double translatedEndPoint = xAxis.java2DToValue((float) panEndPoint.getX(), scaledDataArea,
						hvp.getDomainAxisEdge());
				double dX = translatedStartPoint - translatedEndPoint;

				double oldMin = xAxis.getLowerBound();
				double newMin = oldMin + dX;

				double oldMax = xAxis.getUpperBound();
				double newMax = oldMax + dX;

				xAxis.setLowerBound(newMin);
				xAxis.setUpperBound(newMax);
			}

			// vertical pan (1. Y-Axis)

			XYPlot vvp = cp.getChart().getXYPlot();
			ValueAxis yAxis = vvp.getRangeAxis();

			if (yAxis != null) {
				double translatedStartPoint = yAxis.java2DToValue((float) this.panStartPoint.getY(), scaledDataArea,
						vvp.getRangeAxisEdge());
				double translatedEndPoint = yAxis.java2DToValue((float) panEndPoint.getY(), scaledDataArea,
						vvp.getRangeAxisEdge());
				double dY = translatedStartPoint - translatedEndPoint;

				double oldMin = yAxis.getLowerBound();
				double newMin = oldMin + dY;

				double oldMax = yAxis.getUpperBound();
				double newMax = oldMax + dY;

				yAxis.setLowerBound(newMin);
				yAxis.setUpperBound(newMax);
			}

			// for the next time
			this.panStartPoint = panEndPoint;
		}
	}

	public void chartMouseMoved(ChartMouseEvent arg0) {
	}

	public void mouseClicked(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mouseMoved(MouseEvent e) {
	}

	public void mouseWheelMoved(MouseWheelEvent e) {
		if (!(e.isAltDown() || e.isControlDown() || e.isShiftDown())) {
			XYPlot plot = cp.getChart().getXYPlot();
			double xViewSize = plot.getDomainAxis().getUpperBound() - plot.getDomainAxis().getLowerBound();

			double yViewSize = plot.getRangeAxis().getUpperBound() - plot.getRangeAxis().getLowerBound();

			if (e.getWheelRotation() > 0) {
				xViewSize *= -1;
				yViewSize *= -1;
			}

			plot.getDomainAxis().setUpperBound(plot.getDomainAxis().getUpperBound() - xViewSize / 20);
			plot.getDomainAxis().setLowerBound(plot.getDomainAxis().getLowerBound() + xViewSize / 20);
			plot.getRangeAxis().setUpperBound(plot.getRangeAxis().getUpperBound() - yViewSize / 20);
			plot.getRangeAxis().setLowerBound(plot.getRangeAxis().getLowerBound() + yViewSize / 20);
		}
	}

	private void initComponents() {

		center = new javax.swing.JPanel();

		setClosable(true);
		setIconifiable(true);
		setMaximizable(true);
		setResizable(true);

		center.setLayout(new javax.swing.BoxLayout(center, javax.swing.BoxLayout.Y_AXIS));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				center, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				center, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JPanel center;

	// End of variables declaration//GEN-END:variables

	public void chartMouseClicked(ChartMouseEvent arg0) {
		ChartEntity ce = arg0.getEntity();
		if (ce instanceof LegendItemEntity) {
			LegendItemEntity l = (LegendItemEntity) ce;
			l.getSeriesKey();
			XYSeriesCollection col = ((XYSeriesCollection) sc);
			XYSeries s = col.getSeries(l.getSeriesKey());
			JPopupMenu pop = new legendItemEditorPopup(col, s);
			pop.show(cp, arg0.getTrigger().getX(), arg0.getTrigger().getY());
			XYLineAndShapeRenderer lineR = new XYLineAndShapeRenderer(false, true);
			lineR.setSeriesOutlineStroke(0, new BasicStroke(2));
			lineR.setSeriesOutlineStroke(1, new BasicStroke(4));

			xyp.setRenderer(lineR);
			GeneralPath rb = new GeneralPath();
			rb.moveTo(0f, -8f);
			rb.lineTo(0f, 8f);
			rb.moveTo(-8f, 0f);
			rb.lineTo(8f, 0f);
			Shape shape = (Shape) rb;

			lineR.setSeriesPaint(0, Color.black);

			lineR.setSeriesShape(0, shape);

			cp.getChart().getLegend().setVisible(false);
		}
	}
}
